<template>
    <view class="pub_page pub_login">
        <pub-head headerName="login" :headerConfig="headerConfig"></pub-head>
        <div class="page_box page_box_scroll" :style="{ 'height': `calc(100vh - ${headerHeight})` }">
            <div class="text">
                <div class="tli">
                    <image class="logo" mode="aspectFit"
                        src="https://sppcp-1322907932.cos.ap-beijing.myqcloud.com/app/logo.png" />
                    <div class="p_1">能源精灵</div>
                </div>
                <div class="tli">
                    <!-- 微信登录 -->
                    <button v-if="deviceType == 'mp-weixin' && xieyi" class="pub_btn" open-type="getPhoneNumber"
                        @getphonenumber="toWxLogin">手机快捷登录</button>
                    <div v-if="deviceType == 'mp-weixin' && !xieyi" class="pub_btn"
                        @click="$comFun.toast('请同意《用户注册协议》和《隐私协议》')">手机快捷登录</div>

                    <div v-if="deviceType == 'mp-alipay' && xieyi" class="pub_btn" @click="toZfbLogin">支付宝登录</div>
                    <div v-if="deviceType == 'mp-alipay' && !xieyi" class="pub_btn"
                        @click="$comFun.toast('请同意《用户注册协议》和《隐私协议》')">支付宝登录</div>



                    <div class="pub_btn_normal" @click="$comFun.openWin('/pages/login/login_other', { openType: 2 })">手机号登录
                    </div>
                    <div class="pub_btn_normal pub_btn_normal_no" style="margin-top:30rpx" @click="standClick">
                        {{ standing == 'manage' ? '用户登录' : '管理登录' }}
                    </div>
                </div>
                <div class="tli other" v-if="standing == 'manage'">
                    <div class="po_1">
                        <div class="line"></div>其他登录<div class="line"></div>
                    </div>
                    <div @click="$comFun.openWin('/pages/login/login_other', { openType: 1 })">
                        <image class="icono" mode="aspectFit"
                            src="https://sppcp-1322907932.cos.ap-beijing.myqcloud.com/app/img_2.png" />
                        <div class="po_2">账号登录</div>
                    </div>
                </div>
            </div>
            <div class="xieyi" :style="{ 'bottom': `${statusBottomHeight}px` }">
                <image @click="xieyi = !xieyi" class="icon" mode="aspectFit"
                    :src="xieyi ? 'https://sppcp-1322907932.cos.ap-beijing.myqcloud.com/app/img_49.png' : 'https://sppcp-1322907932.cos.ap-beijing.myqcloud.com/app/img_5.png'" />
                同意并接受 <div class="link" @click="$comFun.openWin('/pages/mine/document', { noticeId: 14 })">《用户注册协议》</div>
                <div class="link" @click="$comFun.openWin('/pages/mine/document', { noticeId: 15 })">《隐私协议》</div>
            </div>
        </div>
    </view>
</template>

<script>
export default {
    data() {
        return {
            headerConfig: {
                left_content: '',
            },
            headerHeight: getApp().globalData.headerHeight,
            statusBottomHeight: getApp().globalData.statusBottomHeight,
            deviceType: getApp().globalData.deviceType,
            standing: 'user',//登录身份  user用户  manage管理
            xieyi: false,


            loginErr: '能源精灵将竭诚为您服务！请联系商户管理员为您开通账户，我们将尽快为您处理！'
        };
    },
    onLoad() {
        // 支付宝小程序设置状态栏文字颜色
        this.$comFun.setZfbNavigationBar();

        if (this.$comFun.getUserToken()) {
            this.openIndex();
        }
    },
    methods: {
        standClick() {
            this.standing = this.standing == 'manage' ? 'user' : 'manage';
            this.$store.dispatch('setStanding', this.standing);
            uni.setStorageSync('userStanding', this.standing);
        },
        openIndex() {
            // console.log(this.$store.state.standing);
            uni.switchTab({
                url: '/pages/base/index'
            });
        },
        async toWxLogin(e) {
            const that = this;
            if (e.detail?.errMsg == 'getPhoneNumber:ok') {
                // console.log(e.detail);
                wx.checkSession({
                    success() {
                        //session_key 未过期，并且在本生命周期一直有效
                        that.sureWxLogin(e);
                    },
                    fail() {
                        // session_key 已经失效，需要重新执行登录流程
                        uni.setStorageSync('wxLoginInfo', '');
                        wx.login({
                            success: async (wxRes) => {
                                // console.log(wxRes);
                                if (wxRes.code) {
                                    const res = await that.$request.post('mobile/wxLogin', {
                                        code: wxRes.code,
                                    });
                                    // console.log(res);
                                    if (res.code == 200) {
                                        uni.setStorageSync('wxLoginInfo', res.data);
                                        that.sureWxLogin(e);
                                    }
                                } else {
                                    that.$comFun.toast(that.loginErr, 3000);
                                }
                            }
                        })
                    }
                })
            } else {
                that.$comFun.toast(that.loginErr, 3000);
            }
        },
        async sureWxLogin(e) {
            const that = this;
            const wxLoginInfo = uni.getStorageSync('wxLoginInfo');
            if (wxLoginInfo) {
                const res_1 = await that.$request.post('mobile/wxGetPhoneNumber', {
                    sessionKey: wxLoginInfo.session_key,
                    encryptData: e.detail.encryptedData,
                    iv: e.detail.iv,
                    wechat_openid: wxLoginInfo.openid
                });
                // console.log(res_1);
                that.$comFun.closeLoading();
                if (res_1.code == 200) {
                    that.$comFun.toast(res_1.msg);
                    that.$comFun.setUserToken(res_1.data.token);
                    that.openIndex();
                } else {
                    that.$comFun.toast(res_1.msg, 3000);
                }
            } else {
                that.$comFun.toast(that.loginErr, 3000);
            }
        },
        toZfbLogin() {
            const that = this;
            my.getAuthCode({
                scopes: 'auth_user',
                success: async res => {
                    console.log(res);
                    if (res.authCode) { }
                },
                fail: err => {
                    that.$comFun.toast('登录失败！' + err);
                }
            });
        }
    },
};
</script>

<style lang="scss" scoped>
@import "@/common/css/login.scss";
</style>
